package login.Action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import login.Model.authenticate;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class actLogin extends ActionSupport{
	
	private String loginUsername;
	private String loginPassword;
	private String cmdbutton;
	private String studentName;
	private String studentID;
	private String idloginMaster;
	private String searchrollno;
	private ArrayList<authenticate> llist = new ArrayList<authenticate>();
	
	public String getSearchrollno() {
		return searchrollno;
	}

	public void setSearchrollno(String searchrollno) {
		this.searchrollno = searchrollno;
	}
	
	public String getIdloginMaster() {
		return idloginMaster;
	}

	public void setIdloginMaster(String idloginMaster) {
		this.idloginMaster = idloginMaster;
	}

	public ArrayList<authenticate> getLlist() {
		return llist;
	}

	public void setLlist(ArrayList<authenticate> llist) {
		this.llist = llist;
	}

	public String getStudentID() {
		return studentID;
	}

	public void setStudentID(String studentID) {
		this.studentID = studentID;
	}

	public String getStudentName() {
		return studentName;
	}

	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

	public String getCmdbutton() {
		return cmdbutton;
	}

	public void setCmdbutton(String cmdbutton) {
		this.cmdbutton = cmdbutton;
	}

	public String getLoginUsername() {
		return loginUsername;
	}

	public void setLoginUsername(String loginUsername) {
		this.loginUsername = loginUsername;
	}

	public String getLoginPassword() {
		return loginPassword;
	}

	public void setLoginPassword(String loginPassword) {
		this.loginPassword = loginPassword;
	}

	public String execute(){
		//System.out.println("m in here");
		return "login";  // opens the login page
	}
	
	public String logincheck() throws SQLException{
		Map session = ActionContext.getContext().getSession();
		
		if(this.cmdbutton.equals("Login")){
			if(authenticate.checkAuthentication(this.loginUsername, this.loginPassword) == 1){
				this.studentName = (String) session.get("sessionstudentname");
				return "loginsuccessstudent";  // opens the student main page
			}
			else if(authenticate.checkAuthentication(this.loginUsername, this.loginPassword) == 2){
				return "loginsuccessadmin";  // open the admin main page
			}
			else{
				addActionError(getText("Incorrect user id or password!"));
				return "exit";  // reopens the login page
			}
		}
		else{
			
			return "exit";
		}
	}
	
	public String showadduser(){
		return "success";  // opens the add user jsp page to add a new user
	}
	
	public String adduser() throws SQLException{
		if(this.cmdbutton.equals("Save")){
			if(authenticate.addauthentication(this.loginUsername, this.loginPassword, this.studentID) == 1){
				return "addusersuccess";  // reloads the same page for a new entry insertion successful
			}
			else{
				return "error";  // opens the admin main page if some error occur in insertion of user
			}
		}
		else if(this.cmdbutton.equals("Exit")){
			return "error"; // opens the admin main page
		}
		else{
			this.llist = authenticate.findall("");
			return "listall"; // open the list all users page for edit or delete
		}
	}
	
	public String searchuser() throws SQLException{
		if(this.cmdbutton.equals("Search")){
			this.llist = authenticate.findall(" and s.studentRollno = " + "'" + this.searchrollno +"'");
			return "searchusersuccess";
		}
		else{
			this.llist = authenticate.findall("");
			return "searchusersuccess";
		}
	}
	
	public String deleteuser() throws SQLException{
		authenticate.deleteauthenticate(this.idloginMaster);
		this.llist = authenticate.findall("");
		return "deleteusersuccess"; // open the list all users page for edit or delete
	}
}
